﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using DTO;
using DAO;
using BUS;
using System.Net.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
namespace Cucre
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ACCOUNT account = new ACCOUNT();
            string account_style = (string)Session["user_style"];
            if (account_style == "4")
            {
                try
                {
                    PROVIDER provider = (PROVIDER)Session["user_login"];
                    account = provider.ACCOUNT;
                    Label1.Text = "Hi " + account.USERNAME.ToUpper();
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('Lỗi tải trang');</script>");
                }
            }
            else
                if (account_style == "1" || account_style == "2" || account_style == "3")
                {
                    try
                    {
                        MEMBER member = (MEMBER)Session["user_login"];
                        account = member.ACCOUNT;
                        lbUser.Text = "Hi " + account.USERNAME;
                        lbUser1.Text = "Hi " + account.USERNAME;
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('Lỗi tải trang');</script>");
                    }
                }
                else
                    account = null;

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            string keyword = Request.Form["keyword"].ToString();
            string url = "SearchResult.aspx?keyword=" + keyword;
            Response.Redirect(url);
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            string email = Request.Form["mail"].ToString();
            if (email == null || email == "" || !email.Contains('@') || !email.Contains('.') || email.Contains(' '))
            {
                Response.Write("<script>alert('Email không hợp lệ');</script>");
            }
            else
            {
                REGISTER_MAIL regEmail = new REGISTER_MAIL();
                regEmail.EMAIL = email;
                regEmail.CITY = "";
                if (Register_mailBUS.insertMail(regEmail))
                {
                    try
                    {
                        MailMessage msg = new MailMessage();
                        msg.IsBodyHtml = true;
                        msg.From = new MailAddress("muare@yahoo.com", "Admin muare.vn");
                        msg.To.Add(new MailAddress(email));
                        msg.Subject = "Đăng kí nhận mail từ muare.vn";
                        msg.Body = "Bạn đã đăng kí nhận tin mới từ muare.vn";
                        SmtpClient client = new SmtpClient("smtp.mail.yahoo.com.vn", 25);
                        client.Credentials = new NetworkCredential("muare", "admin08");
                        client.EnableSsl = false;
                        client.Send(msg);
                        Response.Write("<script>alert('Đăng kí nhận sản phẩm qua mail thành công');</script>");
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('Đăng kí mail thất bại');</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Đăng kí mail thất bại');</script>");
                }
            }

        }

    }
}
